package club.xyes.zkh.ecommerce.commons.util;

import club.xyes.zkh.ecommerce.commons.context.ApplicationConstants;
import club.xyes.zkh.ecommerce.commons.dto.AccessLogExportData;
import org.apache.poi.hssf.usermodel.*;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Create by 郭文梁 2019/5/7 0007 17:03
 * ExcelUtils
 * Excel相关工具类
 *
 * @author 郭文梁
 * @data 2019/5/7 0007
 */
public class ExcelUtils {
    /**
     * 转换为Excel行数据
     * l
     *
     * @param logs 日志
     * @return List by List
     */
    public static List<List<String>> toColsData(List<AccessLogExportData> logs) {
        return logs.stream().map(log -> {
            List<String> col = new ArrayList<>();
            col.add(log.getShopName());
            col.add(log.getUsername());
            col.add(log.getMobile());
            col.add(String.valueOf(MoneyUtils.fen2Yuan(log.getTotalAmount())));
            col.add(String.valueOf(log.getTotalTimes()));
            col.add(DateTimeUtil.format(log.getLastTime(), ApplicationConstants.DATE_TIME_FORMAT));
            return col;
        }).collect(Collectors.toList());
    }

    /**
     * 转换为Excel文件
     *
     * @param cols    行
     * @param headers 表头
     * @return bytes
     * @throws IOException IO异常
     */
    public static byte[] toExcel(List<String> headers, List<List<String>> cols) throws IOException {
        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFCellStyle headerStyle = headerStyle(workbook);
        HSSFCellStyle bodyStyle = bodyStyle(workbook);
        HSSFSheet sheet = workbook.createSheet();
        setSheetOptions(sheet);
        writeLine(sheet, 0, headers, headerStyle);
        for (int i = 0; i < cols.size(); i++) {
            List<String> col = cols.get(i);
            writeLine(sheet, i + 1, col, bodyStyle);
        }
        return toBytes(workbook);
    }

    /**
     * 设置表格属性
     *
     * @param sheet 表格
     */
    private static void setSheetOptions(HSSFSheet sheet) {
        sheet.setColumnWidth(0, 100 * 256);
        sheet.setColumnWidth(1, 100 * 256);
        sheet.setColumnWidth(2, 100 * 256);
        sheet.setColumnWidth(3, 100 * 256);
        sheet.setColumnWidth(4, 100 * 256);
        sheet.setColumnWidth(5, 100 * 256);
    }

    /**
     * 构建表头样式
     *
     * @param workbook 工作簿
     * @return 样式
     */
    private static HSSFCellStyle headerStyle(HSSFWorkbook workbook) {
        HSSFCellStyle style = workbook.createCellStyle();
        HSSFFont font = workbook.createFont();
        font.setBold(true);
        font.setFontHeight((short) 800);
        style.setFont(font);
        return style;
    }

    /**
     * 构建正文样式
     *
     * @param workbook 工作簿
     * @return 样式
     */
    private static HSSFCellStyle bodyStyle(HSSFWorkbook workbook) {
        HSSFCellStyle style = workbook.createCellStyle();
        HSSFFont font = workbook.createFont();
        font.setFontHeight((short) 600);
        style.setFont(font);
        return style;
    }

    /**
     * 写一行数据
     *
     * @param sheet 、表格对象
     * @param line  行号
     * @param data  数据
     */
    private static void writeLine(HSSFSheet sheet, int line, List<String> data, HSSFCellStyle style) {
        HSSFRow row = sheet.createRow(line);
        for (int f = 0; f < data.size(); f++) {
            String cellValue = data.get(f);
            HSSFCell cell = row.createCell(f);
            setCellOptions(cell, style);
            cell.setCellValue(cellValue);
        }
    }

    /**
     * 设置单元格格式
     *
     * @param cell 单元格
     */
    private static void setCellOptions(HSSFCell cell, HSSFCellStyle style) {
        cell.setCellStyle(style);
    }

    /**
     * 转换为byte[]
     *
     * @param book Excel
     * @return bytes
     * @throws IOException IO异常
     */
    private static byte[] toBytes(HSSFWorkbook book) throws IOException {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        book.write(bos);
        bos.close();
        return bos.toByteArray();
    }
}
